import { initRes, initResCb } from '../data-tol'
import {
  rqtPostDefCodeList,
  rqtGetDefCodeList
} from '@/services/common/defcode'

export function initReqRes(
  cxt,
  list,
  req,
  reqParam,
  cb = res => (cxt[list] = res.data.data)
) {
  initRes(cxt[list], req, reqParam, cb)
}

export function initReqResCb(list, req, reqParam, cb) {
  initResCb(list, req, reqParam, cb)
}

export function initReqCodeRes(
  cxt,
  list,
  reqParam,
  cb = res => (cxt[list] = res.data.data)
) {
  initRes(cxt[list], rqtPostDefCodeList, reqParam, cb)
}

export function initReqCodeResCb(list, reqParam, cb) {
  initResCb(list, rqtPostDefCodeList, reqParam, cb)
}

export function initReqCodesRes(cxt, code) {
  if (cxt && code) {
    for (const key of Object.keys(cxt[code])) {
      initReqCodeRes(cxt[code], key, { codeType: key })
    }
  } else {
    console.warn(`initReqCodesRes => ${cxt} or ${code} is invalid!`)
  }
}

export function initReqCodeListResCb(codeList, cb) {
  if (codeList && codeList.length && cb) {
    initResCb(
      [],
      rqtGetDefCodeList,
      {
        codeType: codeList
      },
      res => {
        if (cb) {
          cb(res)
        }
      }
    )
  } else {
    console.warn(`initReqCodeListResCb => ${codeList} or ${cb} is invalid!`)
  }
}
